# -*- coding: utf-8 -*-
'''
Created on Aug 22, 2012

@author: LONG HOANG GIANG
'''
import os
import pyLib
from pyLib import html2text
import datetime
import re, json


FILE = '/longhoanggiang/thugian/ebooks.xml'
if not os.path.exists(os.path.dirname(FILE)):
    os.makedirs(os.path.dirname(FILE), 0777)
    
    
def readFile(filePath):
    f = open(filePath, 'r')
    data = f.read()
    f.close()
    return data

def getDetail(fileName):
    print fileName
    if re.search(r"(\d)+html", fileName): 
        fileName = re.sub(r"(\d+)html", "\\1.html", fileName)
    filePath = os.path.join(os.path.dirname(FILE), fileName)
    html = readFile(filePath)
    html = html2text.html2text(html, w100=True).strip()
    html = re.sub(ur"<b>\n\s*\n", "<b>", html)
    html = re.sub(ur"\n\s*\n\s*</b>", "</b>", html)
    html = re.sub(ur"(\s*\n\s*){3,}", "\n\n", html)
    html = re.sub(ur"\n", "<br />", html)
    html = re.sub(ur"<br /><br /><b></b><br /><br />", "<br /><hr /><br />", html)
    html = re.sub(ur" {3,}", " ", html)
    return html

def process(folder):
    tree = pyLib.buildTree(pyLib.file_get_content(FILE))
    output = "/longhoanggiang/truyentext/{0}".format(folder)
    pyLib.createIfNotExistsPath(output + "/files/")
    data = []
    for item in tree.xpath("//chapter"):
        filename = item.get('page')
        if filename == None: continue
        name = item.get('name')
        if '-' in name:
            name = name[name.find('-')+1:].strip()
        content = getDetail(filename)
        title = pyLib.toUpper(name)
        fileName = pyLib.crc32unsigned(filename)
        pyLib.gzip(output + "/files/" + fileName, json.dumps(content))
        data.append({'name': title, 'path': fileName})
        print '-------------------------------------'
        print 'Tieu De: {0}'.format(name)
        print '-------------------------------------'
        print content
        print '-------------------------------------'
    pyLib.gzip(output + "/data", pyLib.encryptCipher(json.dumps(data)))

if __name__ == '__main__':
    
    process('chothueny')
    
    print '>Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)
    